package eg.com.iti.common.daoimpl;

// Generated Apr 9, 2010 5:21:53 PM by Hibernate Tools 3.2.2.GA

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import eg.com.iti.common.constant.ExceptionCodes;
import eg.com.iti.common.dao.BaseDao;
import eg.com.iti.common.exception.ITISystemException;


public class BaseDaoImpl implements BaseDao {

	public void saveOrUpdate(Object obj) throws ITISystemException {
		Session session = HibernateUtil.getCurrentSession();
		try {
			session.saveOrUpdate(obj);	
			
		} catch (HibernateException e) {
			throw new ITISystemException(ExceptionCodes.SAVE_FAILED_EXCEPTION);
		}
	}

	public void delete(Object obj) throws ITISystemException {
		Session session = HibernateUtil.getCurrentSession();
		try {
			session.delete(obj);
		} catch (HibernateException e) {
			throw new ITISystemException(ExceptionCodes.DELETE_FAILED_EXCEPTION);
		}
	}
	
	@Override
	public Object find(Class clazz, Integer id) throws ITISystemException {		
		Session session = HibernateUtil.getCurrentSession();		
		try {
			Object object=session.get(clazz, id);
			return object;
		} catch (HibernateException e) {
			throw new ITISystemException(ExceptionCodes.RETRIEVE_FAILED_EXCEPTION);
		}
	}
	
	

	public List findAll(Class clazz) throws ITISystemException {
		Session session = HibernateUtil.getCurrentSession();		
		try {
			List l=null;
			Query query=session.createQuery("from "+clazz.getName());
			l=query.list();
			return l;
		} catch (HibernateException e) {
			throw new ITISystemException(ExceptionCodes.RETRIEVE_FAILED_EXCEPTION);
		}
		
	}

	public void beginTransaction() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getCurrentSession();
		session.beginTransaction();
	}

	public void commit() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getCurrentSession();
		session.getTransaction().commit();
	}

	public void rollback() {
		// TODO Auto-generated method stub
		Session session = HibernateUtil.getCurrentSession();
		session.getTransaction().rollback();
	}

}
